//
//  FLLabel.m
//  BaseRepo
//
//  Created by leo on 2018/8/8.
//  Copyright © 2018年 LEO. All rights reserved.
//

#import "FLLabel.h"

@interface FLLabel ()

@property (nonatomic, strong) NSMutableArray *textMArray;

@end



@implementation FLLabel

#pragma mark- 系统方法重写
- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines {
    CGRect superRect = [super textRectForBounds:bounds limitedToNumberOfLines:numberOfLines];
    superRect.origin.x -= self.contentEdge.left;
    superRect.origin.y -= self.contentEdge.right;
    superRect.size.width += self.contentEdge.left + self.contentEdge.right;
    superRect.size.height += self.contentEdge.top + self.contentEdge.bottom;
    return superRect;
}

- (void)drawTextInRect:(CGRect)rect {
    [super drawTextInRect:UIEdgeInsetsInsetRect(rect, self.contentEdge)];
}



#pragma mark- 公开的方法

- (void)clearAttributeString {
    
    [self.textMArray removeAllObjects];
    self.attributedText = nil;
    
}
- (void)refreshAttributeString {
    
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] init];
    for (NSDictionary *tempDic in self.textMArray) {
        NSMutableAttributedString *tempStr = [[NSMutableAttributedString alloc] initWithString:tempDic[@"text"]
                                                                                    attributes:@{
                                                                                                 NSFontAttributeName : tempDic[@"font"],
                                                                                                 NSForegroundColorAttributeName : tempDic[@"color"]
                                                                                                 }];
        [attStr appendAttributedString:tempStr];
    }
    self.attributedText = attStr;
    
}


- (void)refreshTextWithLineSpace:(CGFloat)lineSpace {
    
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.lineSpacing = self.font.lineHeight - self.font.pointSize;
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:self.text attributes:@{NSParagraphStyleAttributeName:style}];
    self.attributedText = attStr;
}


- (void)addAttributeString:(NSString *)text font:(UIFont *)font fontColor:(UIColor *)color {
    
    NSDictionary *dic = @{
                          @"font" : font,
                          @"color" : color,
                          @"text" : text ? text : @""
                          };
    
    [self.textMArray addObject:dic];
    
    
}



#pragma mark- 懒加载
+ (FLLabel *(^)(void))fl_init {
    return ^(void){
        FLLabel *label = [[FLLabel alloc] init];
        label.contentEdge = UIEdgeInsetsZero;
        return label;
        
    };
}

- (FLLabel *(^)(CGRect))fl_frame {
    return ^(CGRect frame){
        self.frame = frame;
        return self;
    };
}


- (FLLabel *(^)(UIColor *))fl_bgColor {
    return ^(UIColor *bgColor){
        self.backgroundColor = bgColor;
        return self;
    };
}


- (FLLabel *(^)(UIColor *))fl_textColor {
    return ^(UIColor *textColor){
        self.textColor = textColor;
        return self;
    };
}

- (FLLabel *(^)(UIFont *))fl_font {
    return ^(UIFont *font){
        self.font = font;
        return self;
    };
}

- (FLLabel *(^)(NSTextAlignment))fl_textAlignMent {
    return ^(NSTextAlignment textAlignMent){
        self.textAlignment  = textAlignMent;
        return self;
    };
}

- (FLLabel *(^)(NSInteger))fl_numberOfLines {
    return ^(NSInteger numberOfLines){
        self.numberOfLines = numberOfLines;
        return self;
    };
}

- (FLLabel *(^)(NSString *))fl_text {
    return ^(NSString *text){
        self.text = text;
        return self;
    };
}



- (NSMutableArray *)textMArray {
    if (!_textMArray) {
        _textMArray = [NSMutableArray array];
    }
    return _textMArray;
}

@end
